﻿namespace Chameleon.Data
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel.DataAnnotations;
	using System.Data.Entity.Validation;
	using System.Data.Linq;
	using System.Linq;

	/// <summary>
	/// Provides a persistence context for data-bound objects via Linq-To-SQL.
	/// </summary>
		
	public abstract class SqlDepository : DataContext
	{
		#region IDepository

			/// <summary>
			/// Inserts an item into the underlying data context and returns a reference to the item.
			/// </summary>
			/// <typeparam name="TEntity">Type of the item to insert into the data context.</typeparam>
			/// <param name="item">Item to insert into the data context.</param>

			public virtual TEntity Insert<TEntity>( TEntity item ) where TEntity: class
			{
				GetTable<TEntity>().InsertOnSubmit( item );
				return item;
			}


			/// <summary>
			/// Attaches a previously disconnected item to the context.
			/// </summary>
			/// <typeparam name="TEntity">Type of the item to attach to the context.</typeparam>
			/// <param name="item">Item to attach to the data context.</param>

			public virtual void Attach<TEntity>( TEntity item ) where TEntity: class
			{
				GetTable<TEntity>().Attach( item, true );
			}


			/// <summary>
			/// Deletes an item from the underlying data context.
			/// <para>This will not take effect until a commit is performed.</para>
			/// </summary>
			/// <typeparam name="TEntity">Type of the item to delete from the data context.</typeparam>
			/// <param name="item"></param>

			public virtual void Delete<TEntity>( TEntity item ) where TEntity: class
			{
				GetTable<TEntity>().DeleteOnSubmit( item );
			}


			/// <summary>
			/// Submits any outstanding changes to the underlying data context.
			/// </summary>
			
			public virtual void Submit()
			{
				SubmitChanges();
			}

		#endregion


		#region IDisposable

			/// <summary>
			/// Performs tasks associated with freeing, releasing, or resetting unmanaged resources.
			/// </summary>
			/// <param name="disposing">When true, releases unmanaged resources being used by managed components.</param>
			
			protected override void Dispose( bool disposing ) 
			{
				base.Dispose( disposing );
			}

		#endregion


		#region Constructors

			/// <summary>
			/// Initializes a new instance by creating a new instance of a basic data context using the specified connection string.
			/// <para>
			/// This is useful for contexts where the entity mapping is explicitly defined on the entity classes using attributes
			/// from System.Data.Linq.Mapping.
			/// </para>
			/// </summary>
			/// <param name="connection">
			/// One of the following:
			///		A complete SQL Server connection string.
			///		The name of an existing SQL Server Express data file.
			///		The name of a server running a SQL Server (the current user's default database will be used).
			/// </param>
			
			protected SqlDepository( string connection ) : base( connection ) {}

		#endregion
	}
}
